/*******************************************************************************
PaddleControllerCpu.cpp

Notes:
*******************************************************************************/

#include "PaddleControllerCpu.h"

//	PaddleControllerCpu(void)
//		Initializes the paddle controller
PaddleControllerCpu::PaddleControllerCpu(void)
{
	// Initialize the move direction to zero
	m_iMoveDirection = 0;
} // PaddleControllerCpu

//	Update(const sf::Input& input)
//		Updates the state based on user input
//			input - The user input
void PaddleControllerCpu::Update(const sf::Input& input)
{
	// Get the entity list
	std::map<EntityID, Entity*> entityList = EntityManager::GetManager()->GetEntityList();
	std::map<EntityID, Entity*>::iterator item;

	// Find the ball and paddle in the list
	BallEntity* pBall = NULL;
	PaddleEntity* pPaddle = NULL;
	for(item = entityList.begin(); item != entityList.end(); item++)
	{
		if(pBall == NULL)
		{
			pBall = dynamic_cast<BallEntity*>(item->second);
		} // if
		
		if(pPaddle == NULL)
		{
			pPaddle = dynamic_cast<PaddleEntity*>(item->second);

			if(NULL != pPaddle)
			{
				PaddleControllerCpu* pController = dynamic_cast<PaddleControllerCpu*>(pPaddle->GetController());

				if(pController == NULL && pController != this)
				{
					pPaddle = NULL;
				}
			} // if
		} // if
	} // for

	if(NULL != pBall && NULL != pPaddle)
	{
		// Move towards the ball
		if((pBall->GetY() + pBall->GetHeight() / 2.0f) < (pPaddle->GetY() + pPaddle->GetHeight() / 2.0f))
		{
			m_iMoveDirection = -1;
		} // if
		else if((pBall->GetY() + pBall->GetHeight() / 2.0f) > (pPaddle->GetY() + pPaddle->GetHeight() / 2.0f))
		{
			m_iMoveDirection = 1;
		} // else if
		else
		{
			m_iMoveDirection = 0;
		} // else
	} // if
} // Update

//	GetMoveDirection(void)
//		Gets the move direction
int PaddleControllerCpu::GetMoveDirection(void)
{
	// Return the move direction
	return m_iMoveDirection;
} // GetMoveDirection